package com.hxzy.dao.impl;

import com.hxzy.bean.Film;
import com.hxzy.bean.HotFilm;
import com.hxzy.dao.HotFilmDao;
import com.hxzy.service.FilmService;
import com.hxzy.service.HotFilmService;
import com.hxzy.service.impl.FilmServiceImpl;
import com.hxzy.service.impl.HotFilmServiceImpl;
import com.hxzy.util.CommonUtil;
import com.hxzy.util.JdbcUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author nick
 * @description
 * @date create in 2020/9/14
 */
public class HotFilmDaoImpl implements HotFilmDao {
    @Override
    public int save(HotFilm hotFilm) {
        String sql = "INSERT INTO hotfilms(film_id,hottime,weight,state) values (?,?,?,?)";
        Object[] params = {hotFilm.getFilm().getfId(),hotFilm.getHotTime(),hotFilm.getWeight(),hotFilm.getState()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(HotFilm hotFilm) {
        String sql = "UPDATE hotfilms SET film_id=?,hottime=?,weight=?,state=? where id=?";
        Object[] params = {hotFilm.getFilm().getfId(),hotFilm.getHotTime(),hotFilm.getWeight(),hotFilm.getState(), hotFilm.getId()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int remove(Integer integer) {
        String sql = "DELETE FROM hotfilms where id = ?";
        return JdbcUtils.executeUpdate(sql,integer);
    }

    @Override
    public List<HotFilm> queryAll() {
        String sql = "SELECT " +
                " id," +
                " film_id fId," +  //film表的主键，hotFilms表的外键
                " hottime hotTime," +
                " weight," +
                " state" +
                " FROM hotfilms";

        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql);
        FilmService service = new FilmServiceImpl();
        return list.stream().map(t -> map2HotFilm(service, t)).collect(Collectors.toList());
    }

    @Override
    public HotFilm findById(Integer integer) {
        String sql = "SELECT " +
                " id," +
                " film_id fId," +  //film表的主键，hotFilms表的外键
                " hottime hotTime," +
                " weight," +
                " state" +
                " FROM hotfilms" +
                " WHERE id = ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql,integer);
        FilmService service = new FilmServiceImpl();
        Optional<HotFilm> first = list.stream().map(t -> map2HotFilm(service, t)).findFirst();//取第一条
        return first.orElse(null);//有则返回，无则返回null
    }

    /**
     * 将一个map集合中的键值转换为HotFilm的对象
     * @param service
     * @param t
     * @return
     */
    private HotFilm map2HotFilm(FilmService service, Map<String, Object> t) {
        HotFilm hotFilm = new HotFilm();
        int id = ((Long) t.get("id")).intValue();
        LocalDateTime hotTime = CommonUtil.timeStamp2LocalDateTime((Timestamp) t.get("hotTime"));//java.sql.Timestamp
        int weight = (int) t.get("weight");
        int state = (int) t.get("state");
        int fId = ((Long) t.get("fId")).intValue();
        //根据film的主键查询film的对象
        Film film = service.findById(fId);

        //populte,为hotFilm对象的每个属性赋值
        hotFilm.setId(id);
        hotFilm.setHotTime(hotTime);
        hotFilm.setWeight(weight);
        hotFilm.setState(state);
        hotFilm.setFilm(film);
        return hotFilm;
    }

    @Override
    public List<HotFilm> queryHotFilms() {
        return null;
    }
}
